package acwing._1_1AlgorithmBasic._6Greedy;
/**
 * @Project : AlgorithmLearning
 * @Package : ACWing._1_1AlgorithmBasic._6Greedy
 * @File : Q0104.java
 * @Author : WangRuoyu
 * @Date : 2023/4/7 10:57
 */

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;

public class Q0104 {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        String[] words = br.readLine().split(" ");
        int n = Integer.parseInt(words[0]);
        long[] a = new long[n];
        words = br.readLine().split(" ");
        for (int i = 0; i < n; ++i) {
            a[i] = Integer.parseInt(words[i]);
        }
        Arrays.sort(a);

        long res = 0;
        int t = n / 2;
        for (int i = 0; i < n; ++i) {
            res += Math.abs(a[i] - a[t]);
        }
        System.out.println(res);
    }
}

